package com.zxq.shiluwengan.service.impl;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import com.zxq.shiluwengan.lock.LockContainer;
import com.zxq.shiluwengan.mqtt.ChargeMqttClient;
import com.zxq.shiluwengan.service.IDeviceExchange;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DeviceExchange implements IDeviceExchange {


    @Autowired
    ChargeMqttClient chargeMqttClient;




    /**
     * mqtt数据交互 封装mqtt服务
     * @param devId 设备编号
     * @param frame 帧数据
     * @return
     */
    @Override
    public byte[] exchange(String devId, byte[] frame) {
        String pubTopic = "service/"+ devId;
        log.info("pubTopic:{} frame:{}" , pubTopic , HexBin.encode(frame));
        // System.out.println("pubTopic:" + pubTopic);
        chargeMqttClient.publishMessage(pubTopic,frame);
        byte[] result = LockContainer.lockChannel(devId);
        return result;
    }


}
